package org.dreamwork.jasmine2.events;

import org.dreamwork.jasmine2.engine.IClickable;
import org.dreamwork.jasmine2.web.IWebControl;

import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-24
 * Time: 11:41:42
 */
public class ClickableDelegate implements IClickable {
    private final Vector<IClickListener> clickListeners = new Vector<IClickListener> ();

    public synchronized void addClickListener (IClickListener listener) {
        clickListeners.addElement (listener);
    }

    public synchronized void removeClickListener (IClickListener listener) {
        clickListeners.removeElement (listener);
    }

    public void fireControlClicked (IWebControl sender) throws EventException {
        IClickListener[] listeners = getClickListeners ();
        for (IClickListener listener : listeners) {
            listener.onClick (sender);
        }
    }

    public IClickListener[] getClickListeners () {
        Vector<IClickListener> copy = new Vector<IClickListener> ();
        synchronized (clickListeners) {
            copy.addAll (clickListeners);
        }
        IClickListener[] ret = new IClickListener[copy.size ()];
        return copy.toArray (ret);
    }
}
